
package com.compuvest.tree.upload

import org.apache.commons.fileupload.FileItem
import org.apache.commons.fileupload.FileUploadException
import org.apache.commons.fileupload.disk.DiskFileItemFactory
import org.apache.commons.fileupload.servlet.ServletFileUpload

import javax.servlet.ServletConfig

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import groovy.json.JsonSlurper

@WebServlet("/tree/upload/UploadJSON")
class UploadJSON extends HttpServlet {

    final static int XML_SIZE = 8388608 // 8M

    def sql
    def params = [:]
    def files  = [:]

    def void init(ServletConfig config) {
	super.init(config)

        sql = new SQLService()
    }

    def void processRequest(request, response) {

        if(!ServletFileUpload.isMultipartContent(request)) return
        //
        def diskFileItemFactory = new DiskFileItemFactory()
        diskFileItemFactory.setSizeThreshold(XML_SIZE) // the unit is bytes in memory

        def servletFileUpload = new ServletFileUpload(diskFileItemFactory)
        servletFileUpload.setSizeMax(XML_SIZE) // the unit is bytes

        def fileItemsList
        try {
            fileItemsList = servletFileUpload.parseRequest(request)
        }
        catch(e) {
            println "----------UploadJSON------------\n${e}" +
                    ">>>fileItemsList=[${fileItemsList.toString()}]\n" +
                    "--------------------------------\n"
            return
        }
        //println ">>>fileItemsList=[${fileItemsList.toString()}]"

        def jsonText
        for(fileItem in fileItemsList) {
            if(!fileItem.isFormField() && fileItem.getSize()) {
                jsonText = fileItem.getString()
                break
            }
        }

        def cats = new JsonSlurper().parseText(jsonText)
        def type = ''
        def accwareCode1 = '', accwareCode2 = '', accwareCode3 = '', accwareCode4 = '', accwareCode5 = '', accwareCode6 = '', accwareCode7 = ''
        def deep1 = 0, deep2 = 0, deep3 = 0, deep4 = 0, deep5 = 0, deep6 = 0, deep7 = 0
        // default category
        sql.updateCategories('', 'def', 'Rename me', 0, 'C', 'N') // accwareParent,accwareCode,name,etilizeid,type,active -- type= C-category, P-preference, A-AttributeName
        // first lvl
        cats.each { lvl1 ->
            accwareCode1 = 'LEVEL1.' + ++deep1
            //println "1 name:${lvl1.property.name}, child:${lvl1.children}"
            type = (lvl1.property.type == 'Category' ? 'C' : (lvl1.property.type == 'Preference' ? 'P' : (lvl1.property.type == 'AttributeName' ? 'A' : '')))
            sql.updateCategories('', accwareCode1, lvl1.property.name, lvl1.property.id, type, 'Y')
            // second lvl
            if(lvl1.children) {
                lvl1.children.each { lvl2 ->
                    accwareCode2 = 'LEVEL2.' + ++deep2
                    //println "2 name:${lvl2.property.name}, child:${lvl2.children}"
                    type = (lvl2.property.type == 'Category' ? 'C' : (lvl2.property.type == 'Preference' ? 'P' : (lvl2.property.type == 'AttributeName' ? 'A' : '')))
                    sql.updateCategories(accwareCode1, accwareCode2, lvl2.property.name, lvl2.property.id, type, 'Y')
                    // three lvl
                    if(lvl2.children) {
                        lvl2.children.each { lvl3 ->
                            accwareCode3 = 'LEVEL3.' + ++deep3
                            //println "3 name:${lvl3.property.name}, child:${lvl3.children}"
                            type = (lvl3.property.type == 'Category' ? 'C' : (lvl3.property.type == 'Preference' ? 'P' : (lvl3.property.type == 'AttributeName' ? 'A' : '')))
                            sql.updateCategories(accwareCode2, accwareCode3, lvl3.property.name, lvl3.property.id, type, 'Y')
                            // fourth lvl
                            if(lvl3.children) {
                                lvl3.children.each { lvl4 ->
                                    accwareCode4 = 'LEVEL4.' + ++deep4
                                    //println "4 name:${lvl4.property.name}, child:${lvl4.children}"
                                    type = (lvl4.property.type == 'Category' ? 'C' : (lvl4.property.type == 'Preference' ? 'P' : (lvl4.property.type == 'AttributeName' ? 'A' : '')))
                                    sql.updateCategories(accwareCode3, accwareCode4, lvl4.property.name, lvl4.property.id, type, 'Y')
                                    // fifth lvl
                                    if(lvl4.children) {
                                        lvl4.children.each { lvl5 ->
                                            accwareCode5 = 'LEVEL5.' + ++deep4
                                            //println "5 name:${lvl5.property.name}, child:${lvl5.children}"
                                            type = (lvl5.property.type == 'Category' ? 'C' : (lvl5.property.type == 'Preference' ? 'P' : (lvl5.property.type == 'AttributeName' ? 'A' : '')))
                                            sql.updateCategories(accwareCode4, accwareCode5, lvl5.property.name, lvl5.property.id, type, 'Y')
                                            // sixth
                                            if(lvl5.children) {
                                                lvl5.children.each { lvl6 ->
                                                    accwareCode6 = 'LEVEL6.' + ++deep5
                                                    //println "6 name:${lvl5.property.name}, child:${lvl5.children}"
                                                    type = (lvl6.property.type == 'Category' ? 'C' : (lvl6.property.type == 'Preference' ? 'P' : (lvl6.property.type == 'AttributeName' ? 'A' : '')))
                                                    sql.updateCategories(accwareCode5, accwareCode6, lvl6.property.name, lvl6.property.id, type, 'Y')
                                                    // seventh
                                                    if(lvl6.children) {
                                                        lvl6.children.each { lvl7 ->
                                                            accwareCode7 = 'LEVEL7.' + ++deep6
                                                            //println "7 name:${lvl6.property.name}, child:${lvl6.children}"
                                                            type = (lvl7.property.type == 'Category' ? 'C' : (lvl7.property.type == 'Preference' ? 'P' : (lvl7.property.type == 'AttributeName' ? 'A' : '')))
                                                            sql.updateCategories(accwareCode6, accwareCode7, lvl7.property.name, lvl7.property.id, type, 'Y')
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    def void doGet(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    def void doPost(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    String getServletInfo() {
        return "UploadJSON"
    }
    def void destroy() {
        sql = null
    }
}

